#include <stdafx.h>
#include "Shaders.h"

int Shaders::Init(char * fileVertexShader, char * fileFragmentShader)
{
	vertexShader = esLoadShader(GL_VERTEX_SHADER, fileVertexShader);

	if ( vertexShader == 0 )
		return -1;

	fragmentShader = esLoadShader(GL_FRAGMENT_SHADER, fileFragmentShader);

	if ( fragmentShader == 0 )
	{
		glDeleteShader( vertexShader );
		return -2;
	}

	program = esLoadProgram(vertexShader, fragmentShader);

	//finding location of uniforms / attributes
	positionAttribute = glGetAttribLocation(program, "a_posL");	
	
	a_normal = glGetAttribLocation(program, "a_normalL");
	a_binorm = glGetAttribLocation(program, "a_binormL");
	a_tgt = glGetAttribLocation(program, "a_tgtL");
	uv_posAttribute = glGetAttribLocation(program, "a_uv");
	Camera_pos = glGetUniformLocation(program, "u_camera_pos");
	u_texture = glGetUniformLocation(program, "u_texture");
	u_texture0 = glGetUniformLocation(program, "u_texture0");
	u_texture1 = glGetUniformLocation(program, "u_texture1");
	u_texture2 = glGetUniformLocation(program, "u_texture2");
	
	u_timer = glGetUniformLocation(program, "timer");
	colorAttribute = glGetUniformLocation(program, "u_colorL");
	matrix = glGetUniformLocation(program, "matrix");
	WVPMatrixUniform= glGetUniformLocation(program, "wvpMatrix");	
	
	// LightSource
	u_light_pos0 =  glGetUniformLocation(program, "u_light_pos0");
	if(u_light_pos0 >-1)
		u_light_pos0= u_light_pos0;
	u_light_diffuse0 = glGetUniformLocation(program,"u_light_diffuse0");	
	u_specularl0 =  glGetUniformLocation(program, "u_specularl0");
	u_light_ambient0 =  glGetUniformLocation(program, "u_ambientLight0");

	u_light_pos1 =  glGetUniformLocation(program, "u_light_pos1");
	u_light_diffuse1= glGetUniformLocation(program,"u_light_diffuse1");	
	u_specularl1 =  glGetUniformLocation(program, "u_specularl1");
	u_light_ambient1 =  glGetUniformLocation(program, "u_ambientLight1");

	u_light_constantAttenuation = glGetUniformLocation(program,"u_light_constantAttenuation");
	u_light_linearAttenuation = glGetUniformLocation(program,"u_light_linearAttenuation");
	u_light_quadraticAttenuation = glGetUniformLocation(program,"u_light_quadraticAttenuation");
	// point light 1

	

	//Material 
	u_material_shininess = glGetUniformLocation(program, "u_material_shininess");
	u_material_specular = glGetUniformLocation(program, "u_material_specular");


	return 0;
}


Shaders::~Shaders()
{
	glDeleteProgram(program);
	glDeleteShader(vertexShader);
	glDeleteShader(fragmentShader);
	if(m_states)
		delete[] m_states;
	m_states = NULL;
}
void Shaders::cleanUp()
{
	m_states = NULL;
}